//
//  UIButton+category.m
//  SSJC
//
//  Created by xzm on 16/11/8.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "UIButton+category.h"

@interface UIButton ()

@property (nonatomic,copy)NSString *addressString;

@end


@implementation UIButton (category)

/**
 快速创建button
 
 @param rect      坐标
 @param font      字体大小
 @param text      文字内容
 @param textColor 文字颜色
 @param image     图片
 @param action    响应方法
 @param target    响应目标
 
 @return button
 */
+ (instancetype)creatWithFrame:(CGRect)rect
                          font:(UIFont *)font
                          text:(NSString *)text
                     textColor:(UIColor *)textColor
                         image:(UIImage *)image
                        action:(SEL)action
                        target:(id)target{
    
    UIButton *btn = [[UIButton alloc]initWithFrame:rect];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:image forState:0];
    [btn setTitle:text forState:0];
    [btn setTitleColor:textColor forState:0];
    btn.titleLabel.font = font;
    return btn;
}
/**
 快速创建button
 
 @param rect      坐标
 @param font      字体大小
 @param text      文字内容
 @param image     图片
 @param action    响应方法
 @param target    响应目标
 
 @return button
 */
+ (instancetype)creatWithFrame:(CGRect)rect
                          font:(UIFont *)font
                          text:(NSString *)text
                         image:(UIImage *)image
                        action:(SEL)action
                        target:(id)target{
    
    return  [self creatWithFrame:rect
                            font:font
                            text:text
                       textColor:[UIColor whiteColor]
                           image:image
                          action:action
                          target:target];
}

/**
 快速创建button
 
 @param rect   坐标
 @param image  字体大小
 @param action 响应方法
 @param target 响应目标
 
 @return button
 */
+ (instancetype)creatWithFrame:(CGRect)rect
                         image:(UIImage *)image
                        action:(SEL)action
                        target:(id)target{
    
    UIButton *btn = [[UIButton alloc]initWithFrame:rect];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:image forState:0];
    return btn;
}

/**
 快速创建button
 
 @param rect      坐标
 @param font      字体大小
 @param text      文字内容
 @param textColor 文字颜色
 @param action    响应方法
 @param target 响应目标
 
 @return button
 */
+ (instancetype)creatWithFrame:(CGRect)rect
                          font:(UIFont *)font
                          text:(NSString *)text
                     textColor:(UIColor *)textColor
                        action:(SEL)action
                        target:(id)target{
    UIButton *btn = [[UIButton alloc]initWithFrame:rect];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:text forState:0];
    [btn setTitleColor:textColor forState:0];
    btn.titleLabel.font = font;
    return btn;
    
}

/**
 快速创建button
 
 @param rect   坐标
 @param font   字体大小
 @param text   文字内容
 @param action 响应方法
 @param target 响应目标
 
 @return button
 */
+ (instancetype)creatWithFrame:(CGRect)rect
                          font:(UIFont *)font
                          text:(NSString *)text
                        action:(SEL)action
                        target:(id)target{
    
    UIButton *btn = [[UIButton alloc]initWithFrame:rect];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:text forState:0];
    btn.titleLabel.font = font;
    return btn;
}

/**
 图片在上，标题在下（必须在设置完标题跟图片之后调用）
 */
- (void)setImageTopTilteBottom{
    
    [self setImageTopTilteBottomWithHeightSpace:10.0f];
}

- (void)setImageTopTilteBottomWithHeightSpace:(CGFloat)heightSpace{
    
    CGSize imgViewSize,btnSize;
    CGFloat titleWidth;
    
    UIEdgeInsets imageViewEdge,titleEdge;
    
    imgViewSize = self.imageView.bounds.size;
    
    titleWidth = [self.titleLabel.text widthWithFont:self.titleLabel.font height:self.titleLabel.height];
    
    btnSize = self.bounds.size;
    
    imageViewEdge = UIEdgeInsetsMake(heightSpace,0.0, btnSize.height -imgViewSize.height - heightSpace, - titleWidth);
    
    [self setImageEdgeInsets:imageViewEdge];
    
    titleEdge = UIEdgeInsetsMake(imgViewSize.height +heightSpace, - imgViewSize.width, 0.0, 0.0);
    
    [self setTitleEdgeInsets:titleEdge];
}

/**
 标题在上，图片在下（必须在设置完标题跟图片之后调用）
 */
- (void)setTilteTopImageBottom{
    
    [self setTilteTopImageBottomWithHeightSpace:10.0f];
}

- (void)setTilteTopImageBottomWithHeightSpace:(CGFloat)heightSpace{
    
    CGSize imgViewSize;
    CGFloat titleWidth;
    
    UIEdgeInsets imageViewEdge,titleEdge;
    
    imgViewSize = self.imageView.bounds.size;
    
    titleWidth = [self.titleLabel.text widthWithFont:self.titleLabel.font height:self.titleLabel.height];
        
    titleEdge = UIEdgeInsetsMake(-heightSpace-self.titleLabel.bounds.size.height, - imgViewSize.width, 0.0, 0.0);
    
    [self setTitleEdgeInsets:titleEdge];
    
    imageViewEdge = UIEdgeInsetsMake(self.titleLabel.bounds.size.height+heightSpace,0.0, 0.0, - titleWidth);
    
    [self setImageEdgeInsets:imageViewEdge];
    
    
}

/**
 标题在坐，图片在右（必须在设置完标题跟图片之后调用），左右间距默认为10；
 */
- (void)setTitleLeftImageRight{
    
    [self setTitleLeftImageRightWithWidthSpace:5.0f];
}

- (void)setTitleLeftImageRightWithWidthSpace:(CGFloat)widthSpace{
    
    CGFloat titleWidth = [self.titleLabel.text widthWithFont:self.titleLabel.font height:self.titleLabel.height];
    
    CGSize imgViewSize = self.imageView.bounds.size;
    
    [self setImageEdgeInsets:UIEdgeInsetsMake(0, self.width - widthSpace - imgViewSize.width, 0, widthSpace - titleWidth)];
    
    CGFloat titleRightInset = self.width - widthSpace - titleWidth;
    
    if (titleRightInset < widthSpace + imgViewSize.width) {
        
        titleRightInset = widthSpace + imgViewSize.width;
    }
    
    [self setTitleEdgeInsets:UIEdgeInsetsMake(0, (widthSpace - imgViewSize.width), 0, titleRightInset)];
}


/**
 标题在坐，图片在右（必须在设置完标题跟图片之后调用
 
 @param middleSpace 中间间距
 */
- (void)setTitleLeftImageRightWithWidthMiddleSpace:(CGFloat)middleSpace{
    
    CGFloat titleWidth = [self.titleLabel.text widthWithFont:self.titleLabel.font height:self.titleLabel.height];
    
    [self setImageEdgeInsets:UIEdgeInsetsMake(0, titleWidth + middleSpace, 0, -titleWidth - middleSpace)];
    
    [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -middleSpace, 0, middleSpace)];
    
}


@end
